package ws_sax;

import java.io.File;
import javax.management.AttributeList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXAttributeProcessing extends DefaultHandler {

    static String user = null;
    static boolean found = false;

    public static void main(String[] args) {
        if (args.length != 0) {
            System.out.println("Usage: java SAXExample tomcat-users.xml username");
            System.exit(3);
        }
        user = args[1];
        SAXAttributeProcessing saxObject = new SAXAttributeProcessing();

        SAXParserFactory spf = SAXParserFactory.newInstance();
        try {
            SAXParser parser = spf.newSAXParser();
            //parser.parse(new File(args[0]), saxObject);
            parser.parse(new File("tomcat-users.xml"), saxObject);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(2);
        }
        
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equals("user")) {
            String name = attributes.getValue("username");
            if (name.equals(user)) {
                found = true;
                System.out.println("User" + user + " is a registered user on Tomcat Server.");
            }
        }
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("User" + user + " is not cuurently defined on Tomcat Server.");
    }


}
